package cn.lhx.blog.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.lhx.blog.dao.DaoArticCatg;
import cn.lhx.blog.pojo.Article;
import cn.lhx.blog.pojo.ArticleCategory;
import cn.lhx.blog.service.ArtiCatgService;

@Transactional
public class ArtiCatgServiceBean implements ArtiCatgService {
	@Resource(name = "daoArticCatg")
	private DaoArticCatg daoArticCatg;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.lhx.blog.service.bean.ArtiCatgService#saveArtiCatg(cn.lhx.blog.pojo
	 * .ArticleCategory)
	 */
	public ArticleCategory saveArtiCatg(ArticleCategory artiCatg) {
		daoArticCatg.insert(artiCatg);
		return artiCatg;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.lhx.blog.service.bean.ArtiCatgService#modifyArtiCatg(cn.lhx.blog.pojo
	 * .ArticleCategory)
	 */
	public ArticleCategory modifyArtiCatg(ArticleCategory artiCatg) {
		daoArticCatg.update(artiCatg);
		return artiCatg;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.lhx.blog.service.bean.ArtiCatgService#delArticatg(cn.lhx.blog.pojo
	 * .ArticleCategory)
	 */
	public Serializable delArticatg(Serializable id) {
		ArticleCategory category = (ArticleCategory) daoArticCatg
				.getById((Integer) id);
		ArticleCategory defaultCtg = (ArticleCategory) daoArticCatg.getById(1);
		// 将该分类的所有文章，设定为“未分类”,即ID=1的分类
		for (Article aticle : category.getIncludeArticles()) {
			aticle.setCategory(defaultCtg);
		}
		daoArticCatg.delete((Integer) id);
		return id;
	}

	@SuppressWarnings(value = "unchecked")
	public List<ArticleCategory> getAllCategory() {
		List<ArticleCategory> categories = new ArrayList<ArticleCategory>();
		categories = (List<ArticleCategory>) daoArticCatg
				.executeQuery("from ArticleCategory ");
		// 如果为list的size为0，则说明，1.要么是首次使用博客，2.要么就是用户自定义的分类为0
		// 针对1，我们应用新建一个“未分类”
		// 针对2，则需要判断
		//博客初始化后，如果”未分类“里没有文章时，那他就没必要出现了
		if(categories.size()>1){
			ArticleCategory defaultCtg=(ArticleCategory)daoArticCatg.getById(1);
			if(defaultCtg.getIncludeArticles().size()==0){
				categories.remove(0);
			}
		}
		//当博客初始化时
		else if (categories.size() ==0) {
				 
			ArticleCategory defaultCatg = new ArticleCategory();
				defaultCatg.setName("未分类");
				defaultCatg.setDescription("未分类文章");
				daoArticCatg.insert(defaultCatg);
				categories.add(defaultCatg);
			
		}
		return categories;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public ArticleCategory getCategoryById(Serializable id) {
		ArticleCategory arCategory = null;
		arCategory = (ArticleCategory) daoArticCatg.getById((Integer) id);
		return arCategory;
	}

	public void changeArticleCount(Serializable id, int incr) {
		// 获得分类
		ArticleCategory arCategory = getCategoryById(id);
		// 文章数+1
		arCategory.setContains(arCategory.getContains() + incr);
		// 更新分类
		daoArticCatg.update(arCategory);

	}

}
